package com.jerry.huihome.module.find.activity;

import android.content.Intent;
import android.support.v4.app.FragmentManager;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.RadioGroup;
import android.widget.RatingBar;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.jerry.huihome.R;
import com.jerry.huihome.base.BaseActivity;
import com.jerry.huihome.base.BaseApplication;
import com.jerry.huihome.base.BaseFragment;
import com.jerry.huihome.constant.Urls;
import com.jerry.huihome.module.find.entity.ForemanDetail;
import com.jerry.huihome.module.find.fragment.ForemanDynamicFragment;
import com.jerry.huihome.module.find.fragment.FroemanWordFragment;
import com.jerry.huihome.module.find.fragment.ForemanWorkFragment;
import com.jerry.huihome.utils.OkHttpUtils;
import com.nostra13.universalimageloader.core.DisplayImageOptions;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by Administrator on 2016/5/13.
 */
public class ForemanDetailActivity extends BaseActivity {
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.tv_foreman_detail_name)
    TextView tvForemanDetailName;
    @BindView(R.id.iv_share_foreman)
    ImageView ivShareForeman;
    @BindView(R.id.iv_foreman_head)
    ImageView ivForemanHead;
    @BindView(R.id.tv_foreman_name)
    TextView tvForemanName;
    @BindView(R.id.ratingBar_foreman)
    RatingBar ratingBarForeman;
    @BindView(R.id.tv_foreman_exp)
    TextView tvForemanExp;
    @BindView(R.id.tv_foreman_case_num)
    TextView tvForemanCaseNum;
    @BindView(R.id.tv_foreman_summary)
    TextView tvForemanSummary;
    @BindView(R.id.rg_detail_foreman)
    RadioGroup rgDetailForeman;
    @BindView(R.id.cb_attention)
    CheckBox checkBox;
    private String url;
    private int pmid;
    private ForemanDetail foremanDetail;
    private DisplayImageOptions options;
    private FragmentManager manager;
    private ForemanWorkFragment workFragment;
    private boolean isAttention = false;
    @Override
    protected int getLayoutId() {
        return R.layout.activity_foreman_detail;
    }

    @Override
    protected void initViews() {
        Intent intent = getIntent();
        pmid = intent.getIntExtra("pmid", -1);
        options = BaseApplication.getApplication().getImageOptions();
        manager = getSupportFragmentManager();
        Map<String, String> m = new HashMap<String, String>();
        m.put("pmid", pmid + "");
        workFragment = BaseFragment.newInstance(ForemanWorkFragment.class, m);
    }

    @Override
    protected void loadData() {
        url = Urls.FOREMAN_DETAIL_URL + pmid;
        OkHttpUtils.get(url, new OkHttpUtils.OnGetDataListener() {
            @Override
            public void onResponse(String url, String data) {
                if (data != null) {
                    Gson gson = new Gson();
                    foremanDetail = gson.fromJson(data, ForemanDetail.class);
                    setViews();
                }
            }

            @Override
            public void onFailure(String url, String error) {

            }
        });
    }

    private void setViews() {
        tvForemanDetailName.setText("工长-" + foremanDetail.getBody().getUsername());
        tvForemanName.setText(foremanDetail.getBody().getUsername());
        ratingBarForeman.setRating(foremanDetail.getBody().getTotalScore());
        tvForemanExp.setText(foremanDetail.getBody().getExpYear() + "");
        tvForemanCaseNum.setText(foremanDetail.getBody().getCaseCount() + "");
        BaseApplication.getApplication().getImageLoader().displayImage(foremanDetail.getBody().getAvatar(), ivForemanHead, options);
        String resume = foremanDetail.getBody().getResume();
        if (resume != null) {
            tvForemanSummary.setText(resume);
        }else {
            tvForemanDetailName.setText("暂无个人简介");
        }
        manager.beginTransaction().replace(R.id.fl_check, workFragment).commit();
        rgDetailForeman.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.rb_foreman_dynamic:
                        Map<String, String> params = new HashMap<String, String>();
                        params.put("pmid", pmid + "");
                        ForemanDynamicFragment dynamicFragment = BaseFragment.newInstance(ForemanDynamicFragment.class, params);
                        manager.beginTransaction().replace(R.id.fl_check, dynamicFragment).commit();
                        break;

                    case R.id.rb_foreman_koubei:
                        Map<String, String> map = new HashMap<String, String>();
                        map.put("pmid", pmid + "");
                        FroemanWordFragment wordFragment = BaseFragment.newInstance(FroemanWordFragment.class, map);
                        manager.beginTransaction().replace(R.id.fl_check, wordFragment).commit();
                        break;

                    case R.id.rb_foreman_place:
                        manager.beginTransaction().replace(R.id.fl_check, workFragment).commit();
                        break;
                }
            }
        });
    }

    @OnClick(R.id.iv_back)
    public void onGoBack() {
        finish();
    }

    @OnClick(R.id.cb_attention)
    public void onCheckAttention() {
        if (isAttention == false){
            Toast.makeText(this,"关注成功",Toast.LENGTH_LONG).show();
            isAttention = true;
        }else {
            Toast.makeText(this,"取消关注成功",Toast.LENGTH_LONG).show();
            isAttention = false;
        }



    }
}
